/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hcht.xrlh.jcxx.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.hcht.xrlh.jcxx.entity.DrJcxx;
import com.hcht.xrlh.jcxx.dao.DrJcxxDao;
import com.hcht.xrlh.jcxx.entity.DrJcxz;
import com.hcht.xrlh.jcxx.dao.DrJcxzDao;
import com.hcht.xrlh.jcxx.entity.DrJczfjl;
import com.hcht.xrlh.jcxx.dao.DrJczfjlDao;

/**
 * 检查任务Service
 * @author lrz
 * @version 2016-12-05
 */
@Service
@Transactional(readOnly = true)
public class DrJcxxService extends CrudService<DrJcxxDao, DrJcxx> {

	@Autowired
	private DrJcxzDao drJcxzDao;
	@Autowired
	private DrJczfjlDao drJczfjlDao;
	
	public DrJcxx get(String id) {
		DrJcxx drJcxx = super.get(id);
		drJcxx.setDrJcxzList(drJcxzDao.findList(new DrJcxz(drJcxx)));
		drJcxx.setDrJczfjlList(drJczfjlDao.findList(new DrJczfjl(drJcxx)));
		return drJcxx;
	}
	
	public List<DrJcxx> findList(DrJcxx drJcxx) {
		return super.findList(drJcxx);
	}
	
	public Page<DrJcxx> findPage(Page<DrJcxx> page, DrJcxx drJcxx) {
		return super.findPage(page, drJcxx);
	}
	
	@Transactional(readOnly = false)
	public void save(DrJcxx drJcxx) {
		super.save(drJcxx);
		for (DrJcxz drJcxz : drJcxx.getDrJcxzList()){
			if (drJcxz.getId() == null){
				continue;
			}
			if (DrJcxz.DEL_FLAG_NORMAL.equals(drJcxz.getDelFlag())){
				if (StringUtils.isBlank(drJcxz.getId())){
					drJcxz.setDrJcxx(drJcxx);
					drJcxz.preInsert();
					drJcxzDao.insert(drJcxz);
				}else{
					drJcxz.preUpdate();
					drJcxzDao.update(drJcxz);
				}
			}else{
				drJcxzDao.delete(drJcxz);
			}
		}
		for (DrJczfjl drJczfjl : drJcxx.getDrJczfjlList()){
			if (drJczfjl.getId() == null){
				continue;
			}
			if (DrJczfjl.DEL_FLAG_NORMAL.equals(drJczfjl.getDelFlag())){
				if (StringUtils.isBlank(drJczfjl.getId())){
					drJczfjl.setDrJcxx(drJcxx);
					drJczfjl.preInsert();
					drJczfjlDao.insert(drJczfjl);
				}else{
					drJczfjl.preUpdate();
					drJczfjlDao.update(drJczfjl);
				}
			}else{
				drJczfjlDao.delete(drJczfjl);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(DrJcxx drJcxx) {
		super.delete(drJcxx);
		drJcxzDao.delete(new DrJcxz(drJcxx));
		drJczfjlDao.delete(new DrJczfjl(drJcxx));
	}
	
}